package com.freight.tws.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.freight.common.core.exception.ServiceException;
import com.freight.common.core.utils.StringUtils;
import com.freight.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.freight.common.core.web.controller.BaseServiceImpl;
import com.freight.tws.mapper.DriverBankMapper;
import com.freight.tws.domain.DriverBank;
import com.freight.tws.service.IDriverBankService;

import java.util.List;

/**
 * 司机银行卡信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-14
 */
@Service
public class DriverBankServiceImpl extends BaseServiceImpl<DriverBankMapper, DriverBank> implements IDriverBankService {

    @Autowired
    private DriverBankMapper driverBankMapper;

    /**
     * 设为默认
     * @param id
     * @return
     */
    @Override
    public boolean setDefault(Long id) {
        List<DriverBank> driverBanks = driverBankMapper.selectList(new LambdaQueryWrapper<DriverBank>().eq(DriverBank::getDriverId, SecurityUtils.getUserId()));
        if(driverBanks.size() == 0) {
            throw new ServiceException("银行卡不存在");
        }
        DriverBank driverBank = driverBanks.stream().filter(d -> d.getId().equals(id)).findAny().orElse(null);
        if(StringUtils.isNull(driverBank)) {
            throw new ServiceException("银行卡不存在");
        }
        for (DriverBank bank : driverBanks) {
            bank.setIsDefault(bank.getId().equals(id) ? "1" : "0");
        }
        return this.updateBatchById(driverBanks);
    }

}
